package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {

        dishService.addDish(dishDTO);

        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult<DishVO>> selectPage(DishPageQueryDTO dishPageQueryDTO) {
        return Result.success(dishService.selectPage(dishPageQueryDTO));
    }

    @DeleteMapping()
    @ApiOperation("删除菜品")
    //使用可变参数或者集合数组接收
    public Result<String> delete(@RequestParam() Long ... ids) {
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品，用于修改的回显数据
     */
    @GetMapping("/{dishId}")
    @ApiOperation("根据id查询菜品以及相关的口味")
    //使用可变参数或者集合数组接收
    public Result<DishVO> delete(@PathVariable("dishId") Long dishId) {

        //查询菜品表
        Dish byId = dishService.getById(dishId);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byId, dishVO);
        //查询口味表
        List<DishFlavor> dishFlavors = Db.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dishId)
                .list();
        dishVO.setFlavors(dishFlavors);

        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @Transactional()
    public Result<String> updateDish(@RequestBody DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishService.update(dish, new QueryWrapper<Dish>().eq("id", dishDTO.getId()));
        //删除口味再插入
        Db.remove(new QueryWrapper<>(new DishFlavor()).eq("dish_id", dishDTO.getId()));
        if (dishDTO.getFlavors() == null) {

            return Result.success();

        }
        dishDTO.getFlavors().forEach(flavor -> flavor.setDishId(dishDTO.getId()));

        Db.saveBatch(dishDTO.getFlavors());

        return Result.success();

    }


}
